/*
 * @Author: your name
 * @Date: 2020-12-30 09:51:58
 * @LastEditTime: 2021-01-12 20:21:53
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \heimamm_mobile\src\router\index.js
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '../utils/token'
import store from '../store/index'
import { auInfo } from '../api/my'

Vue.use(VueRouter)

const routes = [
  // {
  //   path: '/',
  //   redirect: '/login'
  // },
  {
    path: '/login',
    alias: '/', // 别名
    component: () => import('../views/login/login.vue')
  },
  {
    path: '/home',
    component: () => import('..//views/home/home.vue'),
    // 重定向
    redirect: '/home/my',
    // 子路由
    children: [
      {
        path: 'company',
        component: () => import('../views/home/company/company.vue'),
        meta: {
          needTab: true
        }
      },
      {
        path: 'find',
        component: () => import('../views/home/find/find.vue'),
        meta: {
          needTab: true
        }
      },
      {
        path: 'my',
        component: () => import('../views/home/my/my.vue'),
        meta: {
          needLogin: true,
          needTab: true
        }
      },
      {
        path: 'question',
        component: () => import('../views/home/question/question.vue'),
        meta: {
          needLogin: true,
          needTab: true
        }
      },
      {
        path: 'questionInfo',
        component: () => import('../views/home/question/questionInfo.vue'),
        meta: {
          needLogin: true
        }
      },
      {
        path: 'myInfo',
        component: () => import('../views/home/my/myInfo.vue'),
        meta: {
          needLogin: true
        }
      },
      {
        path: 'editInfo',
        component: () => import('../views/home/my/editInfo.vue'),
        meta: {
          needLogin: true
        }
      },
      {
        path: 'shareList',
        component: () => import('../views/home/find/shareList.vue')
      },
      {
        path: 'shareInfo',
        component: () => import('../views/home/find/shareInfo.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由统一报错处理
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

// 前置守卫
router.beforeEach((to, from, next) => {
  // 记录每个页面离开时的scrollTop
  from.meta.scrollTop =
    document.documentElement.scrollTop ||
    window.pageYOffset ||
    document.body.scrollTop

  // if进入页needLogin
  // 没有直接next()
  // if已登录
  // 有 next()
  // 没有进入登录页
  // 有,if(getTOken)
  // 没有就进入登录页
  // 有,调用接口获取用户信息,保存用户信息到vuex
  window.cancelApi('', true) // 切换页面如果接口未完成则全部取消
  if (!to.meta.needLogin) {
    next()
    // 当前用户没有登录,有token,获取用户信息
    if (!store.state.isLogin && getToken('token')) {
      auInfo(true) // true,不需要处理,默认false(需要处理)
        .then(res => {
          store.commit('setUserInfo', res.data)
          store.commit('setIsLogin', true)
        })
        .catch(e => {
          console.log(e)
        })
    }
  } else if (store.state.isLogin) {
    next()
  } else if (!getToken('token')) {
    next('/login?redirect=' + to.fullPath)
  } else {
    auInfo()
      .then(res => {
        store.commit('setUserInfo', res.data)
        store.commit('setIsLogin', true)
        next()
      })
      .catch(e => {
        console.log(e)
      })
  }
})

// 后置守卫
router.afterEach((to, from) => {
  // 每次跳转页面回到顶部
  window.scrollTo(0, 0)
})

export default router
